package com.ruoyi.ic.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.RConst;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ic.mapper.IcGeneralBMapper;
import com.ruoyi.ic.util.DocumentNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ic.domain.IcGeneralB;
import com.ruoyi.ic.mapper.IcGeneralHMapper;
import com.ruoyi.ic.domain.IcGeneralH;
import com.ruoyi.ic.service.IIcGeneralHService;

/**
 * 出库单头，用于保存出库单主要信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-27
 */
@Service
public class IcGeneralHServiceImpl implements IIcGeneralHService 
{
    @Autowired
    private IcGeneralHMapper icGeneralHMapper;
    @Autowired
    private DocumentNum documentNum;
    @Autowired
    private IcGeneralBMapper icGeneralBMapper;

    /**
     * 查询出库单头，用于保存出库单主要信息
     * 
     * @param id 出库单头，用于保存出库单主要信息主键
     * @return 出库单头，用于保存出库单主要信息
     */
    @Override
    public IcGeneralH selectIcGeneralHById(Long id)
    {
        return icGeneralHMapper.selectIcGeneralHById(id);
    }

    /**
     * 查询出库单头，用于保存出库单主要信息列表
     * 
     * @param icGeneralH 出库单头，用于保存出库单主要信息
     * @return 出库单头，用于保存出库单主要信息
     */
    @Override
    public List<IcGeneralH> selectIcGeneralHList(IcGeneralH icGeneralH)
    {
        if(icGeneralH.getInvname()!=null){
            icGeneralH.setInvname(icGeneralH.getInvname()+"%");
        }
        if(icGeneralH.getInvcode()!=null){
            icGeneralH.setInvcode(icGeneralH.getInvcode()+"%");
        }
        return icGeneralHMapper.selectIcGeneralHList(icGeneralH);
    }

    /**
     * 新增出库单头，用于保存出库单主要信息
     * 
     * @param icGeneralH 出库单头，用于保存出库单主要信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIcGeneralH(IcGeneralH icGeneralH)
    {
        Date date = new Date();
        icGeneralH.setCreatetime(date);
        icGeneralH.setDel(RConst.Num.INT_0);
        icGeneralH.setVbillcode(documentNum.getVbillcode(date));
        icGeneralH.setCreater(SecurityUtils.getUsername().toString());

        int rows = icGeneralHMapper.insertIcGeneralH(icGeneralH);
        insertIcGeneralB(icGeneralH);
        return rows;
    }

    /**
     * 修改出库单头，用于保存出库单主要信息
     * 
     * @param icGeneralH 出库单头，用于保存出库单主要信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIcGeneralH(IcGeneralH icGeneralH)
    {
        icGeneralH.setUpdatetime(new Date());
        icGeneralH.setUpdater(SecurityUtils.getUsername());
        icGeneralHMapper.deleteIcGeneralBByHid(icGeneralH.getId());
        insertIcGeneralB(icGeneralH);
        return icGeneralHMapper.updateIcGeneralH(icGeneralH);
    }

    /**
     * 批量删除出库单头，用于保存出库单主要信息
     * 
     * @param ids 需要删除的出库单头，用于保存出库单主要信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIcGeneralHByIds(Long[] ids)
    {
        IcGeneralH icGeneralH = new IcGeneralH();
        icGeneralH.setDel(RConst.Num.INT_1);
        int num = RConst.Num.INT_0;
        for (Long id : ids){
            icGeneralH.setId(id);
            icGeneralHMapper.updateIcGeneralH(icGeneralH);
            num++;
            icGeneralBMapper.updateToDelIcGeneralB(id);
        }
        return num;

        // icGeneralHMapper.deleteIcGeneralBByHids(ids);
        // return icGeneralHMapper.deleteIcGeneralHByIds(ids);
    }

    /**
     * 删除出库单头，用于保存出库单主要信息信息
     * 
     * @param id 出库单头，用于保存出库单主要信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIcGeneralHById(Long id)
    {

        IcGeneralH icGeneralH = new IcGeneralH();
        icGeneralH.setDel(RConst.Num.INT_1);
        icGeneralH.setId(id);
        icGeneralBMapper.updateToDelIcGeneralB(id);
        return icGeneralHMapper.updateIcGeneralH(icGeneralH);

        // icGeneralHMapper.deleteIcGeneralBByHid(id);
        // return icGeneralHMapper.deleteIcGeneralHById(id);
    }

    /**
     * 新增出库单明细，用于保存物资明细信息信息
     * 
     * @param icGeneralH 出库单头，用于保存出库单主要信息对象
     */
    public void insertIcGeneralB(IcGeneralH icGeneralH)
    {
        List<IcGeneralB> icGeneralBList = icGeneralH.getIcGeneralBList();
        Long id = icGeneralH.getId();
        if (StringUtils.isNotNull(icGeneralBList))
        {
            List<IcGeneralB> list = new ArrayList<IcGeneralB>();
            for (IcGeneralB icGeneralB : icGeneralBList)
            {
                IcGeneralB resIGB = icGeneralBMapper.selectIcGeneralBByBid(icGeneralB.getBid());
                icGeneralB.setHid(id);
                if (resIGB == null){
                    icGeneralB.setCreatetime(icGeneralH.getCreatetime());
                    icGeneralB.setCreater(SecurityUtils.getUsername());
                    icGeneralB.setDel(RConst.Num.LONG_0);
                    list.add(icGeneralB);
                }else {
                    resIGB.setUpdater(SecurityUtils.getUsername());
                    resIGB.setUpdatetime(new Date());
                    resIGB.setDel(RConst.Num.LONG_0);
                    icGeneralBMapper.updateIcGeneralB(resIGB);
                }

            }
            if (list.size() > 0)
            {
                icGeneralHMapper.batchIcGeneralB(list);
            }
        }
    }
}
